#include "send_single_area.h"
#include "ui_send_single_area.h"

SendSingleArea::SendSingleArea(QWidget* parent) : QWidget(parent), ui(new Ui::SendSingleArea)
{
    ui->setupUi(this);

    this->timer=new QTimer(this);
    timer->stop();
    timer->setTimerType(Qt::PreciseTimer);
    //定时到了
    connect(timer,&QTimer::timeout, this,[this](){
        QString dataString = ui->plainTextEdit->toPlainText();
        if(dataString.isEmpty())
        {
            return;
        }
        emit requestToSend(dataString);
    });

    //点击发送按钮
    connect(ui->pushButton_Send, &QPushButton::clicked, this, [this]() {
        QString dataString = ui->plainTextEdit->toPlainText();
        if(dataString.isEmpty())
            {
            return;
        }
        emit requestToSend(dataString);
    });
}

SendSingleArea::~SendSingleArea()
{
    delete ui;
}

void SendSingleArea::setSomeComponents(bool checked)
{
    ui->pushButton_Send->setEnabled(checked);//串口打开时允许发送
}

void SendSingleArea::setAutoSendState(bool checked, int cycle)
{
    if(checked)
    {
        if(this->timer->isActive())
        {
            this->timer->setInterval(cycle);
            qDebug()<<"单项发送区自动发送周期改变";
        }
        else
        {
            this->timer->start(cycle);
            qDebug()<<"单项发送区开始自动发送";
        }
    }
    else
    {
        this->timer->stop();
        qDebug()<<"单项发送区停止自动发送";
    }
}



